package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: atp
 * Date: 2022-11-01
 * Time: 17:52
 */
public class DelOperation implements IOOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        System.out.println("请输入你要删除的书名: ");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        //遍历数组,是否有要删的书,记录下标
        int index = -1;
        int currentSize = bookList.getUseSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getPos(i);
            if(name.equals(book.getName())) {
                index= i;
                break;
            }
        }
        if(index == -1) {
            System.out.println("没有你要删除的书!");
            return;
        }
        for (int i = index; i < currentSize - 1 ; i++) {
            Book book = bookList.getPos(i+1);
            bookList.setBooks(book,i);
        }
        //每次删除置空
        bookList.setBooks(null,currentSize-1);

        bookList.setUseSize(currentSize-1);
        System.out.println("删除成功!");
    }
}
